package org.jeecg.modules.assets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.assets.dto.OptionPreliminaryAssetsItemDTO;
import org.jeecg.modules.assets.entity.AssetsItem;
import org.jeecg.modules.assets.entity.AssetsPreliminaryResults;
import org.jeecg.modules.assets.entity.AssetsPreliminaryResultsItem;
import org.jeecg.modules.assets.mapper.AssetsPreliminaryResultsItemMapper;
import org.jeecg.modules.assets.service.IAssetsPreliminaryResultsItemService;
import org.jeecg.modules.assets.service.IAssetsPreliminaryResultsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 初盘结果管理
 * @Author: jeecg-boot
 * @Date: 2023-12-12
 * @Version: V1.0
 */
@Service
public class AssetsPreliminaryResultsItemServiceImpl extends ServiceImpl<AssetsPreliminaryResultsItemMapper, AssetsPreliminaryResultsItem> implements IAssetsPreliminaryResultsItemService {


    @Autowired
    private AssetsPreliminaryResultsItemMapper assetsPreliminaryResultsItemMapper;
    @Autowired
    @Lazy
    private IAssetsPreliminaryResultsService assetsPreliminaryResultsService;

    @Override
    public Page<AssetsPreliminaryResultsItem> getAssetsItem(Page page, String realname, String unionId, String inventoryStatus, Integer preliminaryStatus,String company,Integer applyId,String assetsName) {

        return assetsPreliminaryResultsItemMapper.getAssetsItem(page, realname, unionId, inventoryStatus, preliminaryStatus,company,applyId,assetsName);

    }

    @Override
    public int getHaveNum(String realname, String uniqueId,String company) {
        return assetsPreliminaryResultsItemMapper.getHaveNum(realname, uniqueId,company);

    }

    @Override
    public int getHaveNumDept(Integer id) {
        if (id != null) {
            return assetsPreliminaryResultsItemMapper.getHaveNumDept(id);
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void updateAssetsList(List<AssetsPreliminaryResults> list) {
        list.stream().forEach(item -> item.setStatus("1"));
        assetsPreliminaryResultsService.updateBatchById(list);
    }

    /**
     * 获取初盘明细
     *
     * @param optionPreliminaryAssetsItem
     * @return
     */
    @Override
    public AssetsPreliminaryResultsItem getPrinallItemByCodeAndUser(OptionPreliminaryAssetsItemDTO optionPreliminaryAssetsItem) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("asset_coding", optionPreliminaryAssetsItem.getAssetCoding());
        qw.eq("userer", optionPreliminaryAssetsItem.getUserName());
        qw.eq("company",optionPreliminaryAssetsItem.getCompany());
        if(StringUtils.isNotEmpty(optionPreliminaryAssetsItem.getUniqueId())){
            qw.eq("unique_id", optionPreliminaryAssetsItem.getUniqueId());
        }else {
            qw.eq("apply_id", optionPreliminaryAssetsItem.getApplyId());
        }
        AssetsPreliminaryResultsItem assetsPreliminaryResultsItem = assetsPreliminaryResultsItemMapper.selectOne(qw);
        return assetsPreliminaryResultsItem;
    }
}
